﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowState(Qt::WindowMaximized);
    setAttribute(Qt::WA_DeleteOnClose);
    this->setWindowFlags(Qt::WindowMinMaxButtonsHint|Qt::WindowCloseButtonHint);
    ui->label_camera_show->setHidden(true);
    //绑定相机和对象
    cameraOperate.Init(&m_cGrabber_camera);

    cameraThread = new CameraThread (&cameraOperate);

    qRegisterMetaType<cv::Mat>("cv::Mat");
    connect(cameraThread, SIGNAL(showMat_sig(cv::Mat)), this, SLOT(showMat_slot(cv::Mat)));

    chart = ui->graphicsView_chart->chart();
    // 设置顶部标题
    chart->setTitle( QStringLiteral("分离度统计图"));

    // 设置字体
    QFont titleFont;
    titleFont.setFamily("黑体");
    titleFont.setPointSize(10);
    titleFont.setBold(true);

    // 设置图表标题的字体
    chart->setTitleFont(titleFont);
    //创建折线序列
     series0=new QLineSeries();

     series0->setName(QStringLiteral("激光分离（px）"));

     chart->addSeries(series0);//将序列添加到图表

     ui->pushButton_start->setHidden(true);
     ui->pushButton->setHidden(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

//打开相机
void MainWindow::on_pushButton_openCanera_clicked()
{
    //打开相机
    cameraOperate.setDisplayParameter(( HWND )ui->label_camera_show->winId(),ui->label_camera_show->width(),ui->label_camera_show->height());
    //cameraOperate.setDisplayParameter(( HWND )ui->label_camera_show->winId(),0,0);
    cameraOperate.showDevicePage();
    cameraOperate.display();
    Mat mat;
    if(cameraOperate.getSingleImg2Mat(&mat)==true){
        qDebug()<<"sssss";
        cameraThread->isStopThread = false;
        cameraThread->start();
    }
}

void MainWindow::showMat_slot(Mat mat)
{
    cv::Mat Rgb_src;
    QImage Img_src;
    if (mat.channels() == 3)//RGB Img
    {
        cvtColor(mat, Rgb_src, COLOR_BGR2RGB);//颜色空间转换
        Img_src = QImage((const uchar*)(Rgb_src.data), Rgb_src.cols, Rgb_src.rows, Rgb_src.cols * Rgb_src.channels(), QImage::Format_RGB888);
    }
    else//Gray Img
    {
        Img_src = QImage((const unsigned char*)(mat.data), mat.cols, mat.rows, mat.cols*mat.channels(), QImage::Format_Grayscale8);
    }
    //将QImage的大小收缩或拉伸，与label的大小保持一致。这样label中能显示完整的图片
   QImage imageScale_src = Img_src.scaled(QSize(ui->label_show_img->width(),
   ui->label_show_img->height()));
   QPixmap pixmap_src = QPixmap::fromImage(imageScale_src);
   //ui->label_show_img->setPixmap(pixmap);
   ui->label_camera_show->setPixmap(pixmap_src);



   std::vector<cv::Point> points_yuan ;
   points_yuan = algorithModule.getGrayCenterPoints_gravity(mat);

   std::vector<cv::Point> points ;
   for (int i=0;i<points_yuan.size();i++) {
        Point p = points_yuan.at(i);
        if(p.x > 20){
            points.push_back(p);
        }
   }


   if(points.size()<10){
       return;
   }
   Vec4f line;

   algorithModule.fitLine(points,line);//拟合直线

   Point start_20,start_50,start_80;
   Point end_20,end_50,end_80;
   int width_20 = algorithModule.getLineWidth(mat,line,(int)mat.rows*0.2,start_20,end_20);
   int width_50 = algorithModule.getLineWidth(mat,line,(int)mat.rows*0.5,start_50,end_50);
   int width_80 = algorithModule.getLineWidth(mat,line,(int)mat.rows*0.8,start_80,end_80);

   Mat showMat;
   cvtColor(mat,showMat,COLOR_GRAY2BGR);
   for (int i=0;i<points.size();i++) {
       circle(showMat, points.at(i), 1, Scalar(0, 255, 0), 2);//画点，其实就是实心圆
   }
   algorithModule.showLineFitting(showMat,line);
   // 绘制直线
   cv::line(showMat, start_20, end_20, cv::Scalar(255, 0, 0), 5, 8, 0);
   cv::line(showMat, start_50, end_50, cv::Scalar(255, 0, 0), 5, 8, 0);
   cv::line(showMat, start_80, end_80, cv::Scalar(255, 0, 0), 5, 8, 0);

   cv::putText(showMat,(QString::number(width_20)+"px").toStdString(),end_20,FONT_HERSHEY_TRIPLEX,1.5,cv::Scalar(255, 0, 0),2);
   cv::putText(showMat,(QString::number(width_50)+"px").toStdString(),end_50,FONT_HERSHEY_TRIPLEX,1.5,cv::Scalar(255, 0, 0),2);
   cv::putText(showMat,(QString::number(width_80)+"px").toStdString(),end_80,FONT_HERSHEY_TRIPLEX,1.5,cv::Scalar(255, 0, 0),2);


   cv::Mat Rgb;
   QImage Img;
   if (showMat.channels() == 3)//RGB Img
   {
       cvtColor(showMat, Rgb, COLOR_BGR2RGB);//颜色空间转换
       Img = QImage((const uchar*)(Rgb.data), Rgb.cols, Rgb.rows, Rgb.cols * Rgb.channels(), QImage::Format_RGB888);
   }
   else//Gray Img
   {
       Img = QImage((const unsigned char*)(showMat.data), showMat.cols, showMat.rows, showMat.cols*showMat.channels(), QImage::Format_Grayscale8);
   }
   //将QImage的大小收缩或拉伸，与label的大小保持一致。这样label中能显示完整的图片
    QImage imageScale = Img.scaled(QSize(ui->label_show_img->width(),
    ui->label_show_img->height()));
    QPixmap pixmap = QPixmap::fromImage(imageScale);
    ui->label_show_img->setPixmap(pixmap);

    ////////////////////折线图

       series0->clear();
       Mat1f diss(1,points_yuan.size());
     //序列添加数值
     for(int i=0;i<points_yuan.size();i++)
     {
         Point p = points_yuan.at(i);
         double dis = algorithModule.get_dis(line,p);
         diss.at<float>(0,i) = dis;
         series0->append(i,dis);
     }
     //计算图像均值以及标准差
     Mat means,stddev;
     meanStdDev(diss,means,stddev,noArray());
     //qDebug()<<means.at<double>(0)<<stddev.at<double>(0);
     double minVal, maxVal;
     Point minLoc, maxLoc;
     // 查找最大值和最小值及其位置
     minMaxLoc(diss, &minVal, &maxVal, &minLoc, &maxLoc);
     ui->label_max->setNum(maxVal);
     ui->label_min->setNum(minVal);
     ui->label_mean->setNum(means.at<double>(0));
     ui->label_stddev->setNum(stddev.at<double>(0));

     //创建坐标轴
     QValueAxis *axisX=new QValueAxis;
     axisX->setRange(0,points_yuan.size());
     axisX->setTitleText("px");

     QValueAxis *axisY=new QValueAxis;

     axisY->setRange(-1*100,100);
     axisY->setTitleText("px");

     chart->setAxisX(axisX,series0);//为序列添加坐标轴
     chart->setAxisY(axisY,series0);

}

void MainWindow::on_pushButton_camera_stteing_clicked()
{
    cameraOperate.showPropertiesPage();
}

void MainWindow::on_pushButton_start_clicked()
{
    Mat mat;
    if(cameraOperate.getSingleImg2Mat(&mat)==true){
        qDebug()<<"sssss";
        cameraThread->isStopThread = false;
        cameraThread->start();
    }
}
//关闭检测
void MainWindow::on_pushButton_clicked()
{
    cameraThread->isStopThread = true;
    m_cGrabber_camera.stopLive();
    m_cGrabber_camera.startLive();
}
